package com.njbdqn.call

import com.njbdqn.util.{HDFSConnection, MySQLConnection}
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions._

/**
  * 全局热卖召回 将数据处理后存放到hdfs
  */
object GlobalHotsellCall {
  def hotSell(spark:SparkSession) = {
    val oitab = MySQLConnection.readMySQL(spark,"orderItems")
    //计算全局热卖商品前100名
    val tjtop100 = oitab.groupBy("good_id").agg(sum("buy_num").alias("sellnum"))
      .orderBy(desc("sellnum")).limit(100)
    //将所有用户的id和推荐前30名笛卡尔积
    //找到所有的用户信息
    val users = MySQLConnection.readMySQL(spark,"customs").select("cust_id")
    //最终全局推荐
    val res = users.crossJoin(tjtop100)
    //并把数据存放到hdfs
    HDFSConnection.writeDataToHDFS("/myshops/dwd_hotsell",res)
    //并且将热卖召回的前10名存放到mysql的hotsell
    val hotsell = tjtop100.limit(10)
    MySQLConnection.writeTable(spark,hotsell,"hotsell")
  }
}
